// Copyright 2012 dmitry.a.novikov@gmail.com
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// CnsZ::DObject declaration


#ifndef _INCLUDED_CNZ_DOMAIN_H_
#define _INCLUDED_CNZ_DOMAIN_H_

#include "object.h"
#include "objectimpl.h"

namespace CnsZ {

  class Handle;
  class Domain;

  class DObjectImpl;

  class DObject : public Object {
  protected:
    DObjectImpl* impl;

    DObject();
    ~DObject();
  public:
    DECLARE_CNSZ_DBCLASS(DObject);

    void addRef() const;
    void release() const;
    int numRefs() const;

    P<Handle> handle() const;

    P<Domain> domain() const;

    void read(Filer& filer);

    void write(Filer& filer) const;

    // Called by a container to which this object has been appended
    virtual void appended();

    // Called by a container from which this object has been unappended to
    virtual void willBeUnappended();

    // Returns "<ClassName:Handle>"
    String toString() const;
  protected:
    friend class ClassDbContainer;

    Filer* undoFiler();

    // Notifications:

    enum {
      WriteDefaultUndo,
      ParitalUndo
    };
    // Must be called in each method implementation before actual modifying data
    void fireWillBeModified(int options = WriteDefaultUndo);
  };

  /*
  class DObjectReactor : public Object {
  public:
    DECLARE_CNSZ_CLASS(ObjectReactor);

    virtual void willBeDestroyed(Object* sender) = 0;

    virtual void modified(const Handle* h) = 0;

    virtual void modifiedSubobject(const Handle* sender, const Object* name) = 0;

    virtual void closed(const Handle& sender) = 0;
  };
  */
  class DDictionaryImpl;

  class DDictionary : public DObject {
  protected:
    DDictionary();
    ~DDictionary();
  public:
    DECLARE_CNSZ_DBCLASS(DDictionary);

    void putAt(const Object* id, Object* object, P<Object>* replaced = 0);

    P<Object> getAt(const Object* id) const;

    void attachToHandleAt(const Object* id, DObject* dobject, P<Object>* replaced = 0);

    P<Handle> getHandleAt(const Object* id) const;

    bool has(const Object* id) const;

    size_t size() const;

    P<ObjectIterator> newIterator(const Object* subsetFrom = 0, const Object* subsetTo = 0) const;

    void read(Filer& filer);

    void write(Filer& filer) const;

    //virtual void addReactor(DictionaryReactor* reactor);

    //virtual void removeReactor(DictionaryReactor* reactor);

    String toString() const;
  };

  /*
  class DictionaryReactor : public ObjectReactor {
  public:
    virtual void added(const Handle* handle);
    virtual void removed(const Handle* handle);

    virtual void willBeModified(const Object* handle);

    virtual void modified(const Handle* handle);
    virtual void canceled(const Handle* handle);
  };
*/

  class Domain : public DDictionary {
  protected:
    Domain();
    ~Domain();
  public:
    DECLARE_CNSZ_DBCLASS(Domain);

    void read(Filer& filer);
    void write(Filer& filer) const;

    virtual P<Handle> attach(DObject* object, Handle* prnt = 0);
  };

  class Handle : public Object {
  public:
    DECLARE_CNSZ_CLASS(Handle);

    virtual P<Domain> domain() const = 0;

    virtual P<Handle> parent() const = 0;

    virtual P<DObject> openR() = 0;

    virtual P<DObject> openW() = 0;
  };

}

#endif //#ifndef _INCLUDED_CNZ_DOMAIN_H_